﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;

public partial class admin_Admin_KeyWordList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            this.GetNewsPages();
        }
        Button1.Attributes.Add("onclick", "return confirm('确定删除所选内容 ?')");
        this.NewsList.Attributes.Add("bordercolor", "#B8D3F1");

    }

    private void GetNewsPages()
    {

        KeyWord getkey = new KeyWord();
        AspNetPager1.RecordCount = getkey.GetKeyCount();
        DataSet dr = getkey.GetKeyPage(AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1), AspNetPager1.PageSize, "getkey");
        DataTable da = dr.Tables["getkey"];
        NewsList.DataSource = da;
        NewsList.DataKeyNames = new string[] { "id" };//主键
        NewsList.DataBind();
        //dr.Clear();
        //da.Clear();
    }

    protected void AspNetPager1_PageChanged(object src, EventArgs e)
    {
        GetNewsPages();
    }
    protected void NewsList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //限制标题换行
        e.Row.Cells[1].Attributes.Add("style", "word-break :break-all ; word-wrap:break-word"); 
        //鼠标换背景色
        int i;
        //执行循环，保证每条数据都可以更新
        for (i = 0; i < NewsList.Rows.Count; i++)
        {
            //首先判断是否是数据行
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#E6F5FA'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
        }
         //删除提醒框
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
             if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[5].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除：\"" + e.Row.Cells[2].Text + "\"吗?')");
            }
        } 

    }
    protected void NewsList_RowEditing(object sender, GridViewEditEventArgs e)
    {
        NewsList.EditIndex = e.NewEditIndex;
        GetNewsPages();
    }

    //删除
    protected void NewsList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        KeyWord delkey = new KeyWord();
        delkey.DelKey(Convert.ToInt32(NewsList.DataKeys[e.RowIndex].Value.ToString()));
        GetNewsPages();
    }

    //更新
    protected void NewsList_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string sTitle=((TextBox)(NewsList.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
        string sUrl=((TextBox)(NewsList.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();
        KeyWord upkey = new KeyWord();
        upkey.UPPKey(sTitle, sUrl,Convert.ToInt32(NewsList.DataKeys[e.RowIndex].Value.ToString()));
        NewsList.EditIndex = -1;
        GetNewsPages();
    }

    //取消
    protected void NewsList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        NewsList.EditIndex = -1;
        GetNewsPages();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {

        KeyWord delkey = new KeyWord();
          for (int i = 0; i < NewsList.Rows.Count; i++) 
           {
               CheckBox cbox = (CheckBox)NewsList.Rows[i].Cells[0].FindControl("del");
               if (cbox.Checked == true) 
               {
                   delkey.DelKey(Convert.ToInt32(NewsList.DataKeys[i].Value));
              }
           }
      GetNewsPages();
      Button2.Text = "全选";

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        CheckBox cb;
        if (Button2.Text == "全选")
        {
            for (int i = 0; i < NewsList.Rows.Count; i++)
            {
                cb = (CheckBox)NewsList.Rows[i].Cells[0].FindControl("del");
                cb.Checked = true;
            }
            Button2.Text = "取消";
        }
        else
        {
            for (int i = 0; i < NewsList.Rows.Count; i++)
            {
                cb = (CheckBox)NewsList.Rows[i].Cells[0].FindControl("del");
                cb.Checked = false;
            }
            Button2.Text = "全选";
        }

    }
}
